//
//  SJNavigationBar.m
//  WSL
//
//  Created by CJ on 2017/10/26.
//  Copyright © 2017年 sanjuetech. All rights reserved.
//

#import "SJNavigationBar.h"


@implementation SJNavItemCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    [self.contentView addSubview:self.itemImageView];
    self.itemImageView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    
}

- (void)setItem:(SJNavItemModel *)item {
    
    _item = item;
    NSString *imageName = [NSString stringWithFormat:@"%@", item.imageUrl];
    _itemImageView.image = [UIImage imageNamed:imageName];
}


#pragma mark - getter

- (UIImageView *)itemImageView{
    
    if (!_itemImageView) {
        
        _itemImageView = [UIImageView new];
        _itemImageView.contentMode = UIViewContentModeCenter;
        
    }
    return _itemImageView;
}

@end


@implementation SJNavigationBar 

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    [self addSubview:self.collectionView];
    self.collectionView.sd_layout.spaceToSuperView(UIEdgeInsetsMake(0, 0, 0, 0));
    [self.collectionView reloadData];
}

#pragma mark - getter

- (SJCustomLayout *)customLayout{
    
    if (!_customLayout) {
        _customLayout = [[SJCustomLayout alloc] init];
        _customLayout.delegate = self;
    }
    return _customLayout;
}

- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.customLayout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        
        if ([_collectionView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
            if (@available(iOS 11.0, *)) {
                _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            } else {
                // Fallback on earlier versions
            }
        }
        
        [_collectionView registerClass:[SJNavItemCell class] forCellWithReuseIdentifier:NSStringFromClass([SJNavItemCell class])];
        
    }
    
    return _collectionView;
}

- (NSMutableArray *)dataArray {
    
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

#pragma mark - SJCustomLayoutDelegate

- (CGRect)collectionView:(UICollectionView *)collectionView layout:(SJCustomLayout *)layout frameOfItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat margin = SJLayoutFrame(10);
    CGFloat itemWH = (SJScreenW - 7 * margin) / 6;

    CGFloat x = margin + (margin + itemWH) * indexPath.row;
    CGFloat y = margin;
    CGFloat w = itemWH;
    CGFloat h = itemWH;
    
    return CGRectMake(x, y, w, h);
}

#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SJNavItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([SJNavItemCell class]) forIndexPath:indexPath];
    SJNavItemModel *item = self.dataArray[indexPath.row];
    cell.item = item;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate respondsToSelector:@selector(navigationBar:didSelectItemAtIndexPath:)]) {
        
        [(id<SJNavigationBarDelegate>)self.delegate navigationBar:self didSelectItemAtIndexPath:indexPath];
    }
}
#pragma mark - ---------- 允许长按 ----------
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

#pragma mark - ---------- didHighlight ----------
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath];
    [collectionView bringSubviewToFront:selectedCell];
    [UIView animateWithDuration:0.25 animations:^{
        selectedCell.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
    }];
}

#pragma mark - ---------- didUnhighlight ----------
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *selectedCell = [collectionView cellForItemAtIndexPath:indexPath];
    [UIView animateWithDuration:0.25 animations:^{
        selectedCell.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
    }];
}

@end
